﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.BLL;
using System.Data;

public partial class Default_Log : WebForm
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlSoftType.DataTextField = "SoftName";
            ddlSoftType.DataValueField = "SoftName";
            ddlSoftType.DataSource = SoftwaresBLL.GetSoftwares();
            ddlSoftType.DataBind();
            ddlSoftType.Items.Insert(0, "请选择...");
            this.btnDeleteAll.Attributes.Add("onclick", "javascript:return confirm('你确认要删除全部用户登录信息吗?')");
            //BindData();
        }
    }

    void BindData()
    {
        string filter = "";
        if (!string.IsNullOrEmpty(txtUserName.Text))
        {
            filter += " UserName like '%" + txtUserName.Text + "%'";
        }
        if (ddlSoftType.SelectedIndex > 0)
        {
            if (string.IsNullOrEmpty(filter))
            {
                filter += " SoftName like '%" + ddlSoftType.SelectedValue + "%'";
            }
            else
            {
                filter += " and SoftName like '%" + ddlSoftType.SelectedValue + "%'";
            }
        }
        if (!string.IsNullOrEmpty(txtAddStartDate.Text) && !string.IsNullOrEmpty(txtAddEndDate.Text))
        {
            try
            {
                SetClientDate(txtAddEndDate.ClientID, DateTime.Parse(txtAddEndDate.Text));
                SetClientDate(txtAddStartDate.ClientID, DateTime.Parse(txtAddStartDate.Text));
            }
            catch { }
            if (string.IsNullOrEmpty(filter))
            {
                filter += " LoginDate >= '" + txtAddStartDate.Text + "' and  LoginDate <= '" + txtAddEndDate.Text + "'";
            }
            else
            {
                filter += " and LoginDate >= '" + txtAddStartDate.Text + "' and  LoginDate <= '" + txtAddEndDate.Text + "'"; ;
            }
        }

        DataTable dt = LoginLogBLL.GetLogsFilter(filter);
        Session["logs"] = dt;
        GridView1.DataKeyNames = new string[] { "AutoID" };
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            //string id = e.CommandArgument as string;
            int id = int.Parse(e.CommandArgument as string);

            GridViewRow row = GridView1.Rows[id];
            string idstr = GridView1.DataKeys[id].Values[0].ToString();

            if (LoginLogBLL.Delete(idstr) > 0)
            {
                Alert("登录删除成功!");
                BindData();
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：此用户[\"" + e.Row.Cells[1].Text + "\"] 登录信息吗?')");
            }
        }
    }


    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DataTable dt = Session["logs"] as DataTable;
        if (dt != null)
        {
            foreach (DataRow row in dt.Rows)
            {
                LoginLogBLL.Delete(row["AutoID"].ToString());
            }
            BindData();
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtUserName.Text = "";
        ddlSoftType.SelectedIndex = 0;
        txtAddStartDate.Text = "";
        txtAddEndDate.Text = "";
    }

    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        if (LoginLogBLL.DeleteAll() > 0)
        {
            Alert("全部数据删除成功!");
            BindData();
        }
    }
}